package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetMealController {

    @Autowired
    private SetMealService setMealService;


    /**
     * 根据分类id和装填来查询套餐数据
     *
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(Long categoryId, Integer status) {

        //调用service
        List<Setmeal> list = setMealService.findCategoryById(categoryId, status);

        return R.success(list);
    }

    /**
     * 删除套餐（包含单个删除和批量删除）
     *
     * @param ids
     * @return
     */
//    public R delete(long [] ids){
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {

        int row = setMealService.delete(ids);
        if (row > 0) {
            return R.success("删除成功~！");
        } else {
            return R.success("删除失败~！");
        }

    }

    /**
     * 分页查询套餐
     *
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam) {

        //1. 调用service
        IPage<SetmealDto> page = setMealService.findPage(pageParam);

        //2. 返回
        return R.success(page);


    }


    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto) {

        //1. 调用service
        int row = setMealService.add(setmealDto);

        //2. 判断
        if (row > 0) {
            return R.success("新增套餐成功！");
        } else {
            return R.error("新增套餐失败！");
        }

    }

    /**
     * 批量停售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R updataStatus(@RequestParam List<Long> ids) {

        int key = setMealService.updataStatus(ids);

        return key > 0 ? R.success("更新成功！") : R.error("更新失败！");
    }
    /**
     * 批量起售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R updataStatus2(@RequestParam List<Long> ids) {

        int key = setMealService.updataStatus2(ids);

        return key > 0 ? R.success("更新成功！") : R.error("更新失败！");
    }

}
